clear all
cd "I:\workdata\706727\Build\Data"
use psychighfreq_AllAges_finalfull

cap drop dsui
gen dsui=(c_liste49=="48")

replace asui=0 if mi(asui) | asui<0
replace dsui=0 if mi(dsui) | dsui<0
gen sui=asui+dsui

replace bamb_s=0 if mi(bamb_s)
replace erp_s=0 if mi(erp_s)
replace st_phosp_s=0 if mi(st_phosp_s)

gen bamb_AnxDep=(bamb_1==3 | bamb_2==3 | bamb_1==4 | bamb_2==4)*bamb_s
gen erp_AnxDep=(erp_1==3 | erp_2==3 | erp_1==4 | erp_2==4)*erp_s
gen PHosp_AnxDep=(st_phosp_1==3 | st_phosp_2==3 | st_phosp_1==4 | st_phosp_2==4)*st_phosp_s
gen psykhosp=bamb_s+erp_s+st_phosp_s

gen byte married=(!mi(efalle))
gen byte famany_fam=((famany_child>0 & !mi(famany_child)) | (famany_par>0 & !mi(famany_par)) | (famany_sib>0 & !mi(famany_sib)))

keep pnr year month firsttherapy koen famany_fam married Dsui sui bamb_AnxDep erp_AnxDep PHosp_AnxDep psykhosp mentaldiag psyk_test psychiatry therapy pois pois_painkiller psykmed adep ssri Dtherapy Dpsychiatry Dpsykhosp Dadep  Danxsleep Dsui Dpois_painkiller

/* Allow for observations after standard analysis window (2015-2019)*/
cap drop _merge
merge 1:1 pnr year month using "J:\workdata\706727\Temp\physicalhealth", keepusing(pnr year month foed_dag docvisit p_docvisit specdoc p_specdoc d_medphyc medphyc p_medphyc d_inpat inpat p_inpat d_outpat outpat p_outpat d_er er p_er psyc p_psyc)
drop _merge

cap drop _merge
merge 1:1 pnr year month using "J:\workdata\706727\Temp\extradata", keepusing(pnr year month sick disab sueduc cash_unemp2 cash_unemp cash_int educhelp sometrans dagp_unemp dagp_act ledyd forre re flexjob skaane intyd cash_dagexp  resf jobafk parleave grad earn inc assets liabilities kontanthj_13 offpens absh workh)
keep if _merge==3
drop _merge


keep if inrange(year,1999,2019)

foreach var in sick disab sueduc cash_unemp2 cash_unemp cash_int educhelp sometrans dagp_unemp dagp_act ledyd forre re flexjob skaane intyd cash_dagexp  resf jobafk parleave {
qui sum year if !mi(`var') 
replace `var'=0 if mi(`var') & inrange(year,r(min),r(max))

gen byte D`var'=(`var'>0 & !mi(`var')) if inrange(year,r(min),r(max))
drop `var'
}
egen Dcash_any=rowmax(Dcash_unemp2 Dcash_unemp Dcash_int Dintyd Dcash_dagexp Deduchelp)
egen Ddisab_any=rowmax(Dledyd Dforre Dre Dflexjob Dskaane Dresf Djobafk Ddisab )
egen Ddagp_any=rowmax(Ddagp_unemp Ddagp_act)
egen Dallnonparleave=rowmax(Dsick Ddisab Dcash_unemp2 Dcash_unemp Dcash_int Deduchelp Ddagp_unemp Ddagp_act Dledyd Dforre Dre Dflexjob Dskaane Dintyd Dcash_dagexp  Dresf Djobafk)
egen Dmixlabortrans=rowmax(Dforre Dre Dflexjob Dskaane Dresf Djobafk)
drop Dcash_unemp Dcash_int Dintyd Dcash_dagexp Deduchelp Dledyd Dforre Dre Dflexjob Dskaane Dresf Djobafk Ddagp_unemp Ddagp_act

cap drop birthyear birthmonth
gen birthyear=year(foed_dag)
gen birthmonth=month(foed_dag)
gen yrm=year+(month-1)/12

foreach var in docvisit specdoc medphyc inpat outpat er psyc {
qui sum year if !mi(`var') 
replace `var'=0 if mi(`var') & inrange(year,r(min),r(max))

cap drop D`var'
gen byte D`var'=(`var'>0 & !mi(`var')) if inrange(year,r(min),r(max))
}
gen wealth=assets-liabilities
foreach var in absh workh inc earn wealth {
qui sum year if !mi(`var') 
replace `var'=0 if mi(`var') & inrange(year,r(min),r(max))
}

sort pnr Dpsyc year month
cap drop firstpsyc
by pnr: gen byte firstpsyc=((Dpsyc==1 & Dpsyc[_n-1]==1 & yrm-yrm[_n-1]>=1) | (Dpsyc==1 & Dpsyc[_n-1]==0)) if pnr[_n-1]==pnr 

replace grad=0 if mi(grad) & year>=2008 & !mi(year)
gen grademp=(grad>80) if !mi(grad)

cap drop age
gen age=year-birthyear  if birthmonth<=month & !mi(month)
replace age=year-birthyear-1 if birthmonth>month & !mi(birthmonth)

cap drop monthdist
gen monthdist=month-birthmonth
replace monthdist=monthdist+12 if monthdist<0 

cap drop agec
gen agec=age+monthdist/12

keep if inrange(agec,8,60)

bysort pnr: gegen maxtherapy=max(firsttherapy)
keep if maxtherapy==1
duplicates drop

/* create duplicates for individuals with more than one first therapy session */
by pnr: egen sumtherapy=sum(firsttherapy)
by pnr: gen numtherapy=sum(firsttherapy)


/* allow only 4 cases per person */
replace numtherapy=. if numtherapy>4 & !mi(numtherapy)

replace numtherapy=. if firsttherapy!=1

sort pnr numtherapy
by pnr: gen yrm1=yrm[1]
by pnr: gen yrm2=yrm[2] if sumtherapy>=2 & !mi(sumtherapy)
by pnr: gen yrm3=yrm[3] if sumtherapy>=3 & !mi(sumtherapy)
by pnr: gen yrm4=yrm[4] if sumtherapy>=4 & !mi(sumtherapy)
by pnr: gen agec1=agec[1]
by pnr: gen agec2=agec[2] if sumtherapy>=2 & !mi(sumtherapy)
by pnr: gen agec3=agec[3] if sumtherapy>=3 & !mi(sumtherapy)
by pnr: gen agec4=agec[4] if sumtherapy>=4 & !mi(sumtherapy)
drop numtherapy

expand sumtherapy
sort pnr year month
by pnr year month: gen dupli=_n

drop if dupli>4

cap drop distyrm
gen distyrm=round(yrm-yrm1,0.001) if dupli==1
replace distyrm=round(yrm-yrm2,0.001) if dupli==2
replace distyrm=round(yrm-yrm3,0.001) if dupli==3
replace distyrm=round(yrm-yrm4,0.001) if dupli==4

gen treatagec=agec1 if dupli==1
replace treatagec=agec2 if dupli==2
replace treatagec=agec3 if dupli==3
replace treatagec=agec4 if dupli==4

gen tyrm=yrm1 if dupli==1
replace tyrm=yrm2 if dupli==2
replace tyrm=yrm3 if dupli==3
replace tyrm=yrm4 if dupli==4

save TherapyLongRunRD, replace
